package cn.kgc.boot.vue.controller;

import cn.hutool.crypto.SecureUtil;
import cn.kgc.boot.vue.entity.User;
import cn.kgc.boot.vue.response.ResponseResult;
import cn.kgc.boot.vue.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 课工场
 * @date 2025/9/8
 * @description 用户模块的控制层
 */
@RestController   //  标识当前类为控制层
@RequestMapping("user")
@CrossOrigin   // 允许跨域请求
public class UserController {

    @Resource
    private UserService userService;

    @RequestMapping("list")   // /user/list
    public ResponseResult userList() {
        //     调用业务层代码 查询数据库
        List<User> list = userService.list();
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(200);
        responseResult.setData(list);
        responseResult.setMessage("查询成功");
        return responseResult;
    }


    // 新增  编辑用户数据接口   post   axios  json
    @RequestMapping("addOrUpdate")
    public ResponseResult addOrUpdateUser(@RequestBody User user) {

        //  新增  编辑    id=null    id=1
        if (user.getId() == null) {   // 新增
            // 设置用户的默认密码
            String defaultPwd = "111111";
            String md5Pwd = SecureUtil.md5(defaultPwd);
            user.setPassword(md5Pwd);
        }

        ResponseResult responseResult = new ResponseResult();
        // 保存数据    id
        try {
            userService.saveOrUpdate(user);
            responseResult.setCode(200);
            responseResult.setMessage("操作成功");
        } catch (Exception e) {
            responseResult.setCode(500);
            responseResult.setMessage("用户名重复...");
        }
        return responseResult;
    }


    // 用户数据分页展示   /user/page
    @RequestMapping("page")
    public ResponseResult userPage(Integer page, Integer size) {

        // 创建分页对象
        Page<User> userPage = new Page<>(page, size);
        userService.page(userPage);

        // 通过分页对象 获取分页数据  总条数  数据记录
        long total = userPage.getTotal();
        List<User> records = userPage.getRecords();

        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(200);
        responseResult.setData(records);
        responseResult.setCount(total);
        return responseResult;

    }


    // 用户删除
    @RequestMapping("delete")
    public ResponseResult deleteUser(@RequestBody List<Integer> ids) {

        ResponseResult responseResult = new ResponseResult();
        // 删除用户信息
        try {
            userService.removeBatchByIds(ids);
            responseResult.setCode(200);
            responseResult.setMessage("删除成功");
        } catch (Exception e) {
            responseResult.setCode(500);
            responseResult.setMessage("删除失败");
        }
        return responseResult;
    }

    @RequestMapping("register")
    public ResponseResult registerUser(@RequestBody User user) {
        System.out.println("user = " + user);
        // 1.  判定用户名是否存在
        String username = user.getUsername();
        // 构建查询条件
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("username", username);   // where  username = lisi
        User one = userService.getOne(userQueryWrapper);
        ResponseResult responseResult = new ResponseResult();
        if (ObjectUtils.isNotNull(one)) {
            responseResult.setCode(110);
            responseResult.setMessage("用户名已被注册");
            return responseResult;
        }
        // 2.  密码加密   md5
        String password = user.getPassword();
        user.setPassword(SecureUtil.md5(password));

        // 3. 保存数据
        try {
            userService.save(user);
            responseResult.setMessage("注册成功,请登录!");
            responseResult.setCode(200);
        } catch (Exception e) {
            e.printStackTrace();
            responseResult.setMessage("服务器异常,注册失败");
            responseResult.setCode(500);
        }

        // 4. 响应结果
        return responseResult;
    }

    @RequestMapping("login")
    public ResponseResult login(@RequestBody User user) {
        // 根据用户名和密码进行数据查询  查询结果不为null  认证通过 否则 认证失败
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("username", user.getUsername())
                .eq("password", SecureUtil.md5(user.getPassword()));

        User one = userService.getOne(userQueryWrapper);
        ResponseResult responseResult = new ResponseResult();
        if (ObjectUtils.isNull(one)) {
            responseResult.setCode(110);
            responseResult.setMessage("用户名或密码错误");
            return responseResult;
        }

        responseResult.setCode(200);
        responseResult.setMessage("认证通过");
        return responseResult;
    }
}
